package com.zheng.cms.rocketmq.rocketmq;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import org.apache.log4j.Logger;

public class OrderProducer {
    protected Logger log = Logger.getLogger(getClass());

    private String producerGroup;

    private String namesrvAddr;

    private String instanceName;

    private int time;

    private int size;

    private DefaultMQProducer producer;



    public DefaultMQProducer getProducer() {
        return producer;
    }

    public OrderProducer(String producerGroup,String namesrvAddr,String instanceName,int time,int size){
        this.producerGroup = producerGroup;
        this.namesrvAddr = namesrvAddr;
        this.instanceName = instanceName;
        this.time = time;
        this.size = size;
    }
    /**
     * DefaultMQProducer
     * 声明并初始化一个producer// 需要一个producer group名字作为构造方法的参数，这里为ordered_producer
     * DefaultMQProducer orderedProducer = new DefaultMQProducer("ordered_producer");
     * // 设置NameServer地址,此处应改为实际NameServer地址，多个地址之间用；分隔
     * orderedProducer.setNamesrvAddr("10.1.54.121:9876;10.1.54.122:9876");
     *
     *  声明并初始化一个producer
     * */
    public void init() throws MQClientException {
        log.info("start init DefaultMQProducer...");
        producer = new DefaultMQProducer(producerGroup);//需要一个producer group名字作为构造方法的参数，这里为ordered_producer
        producer.setNamesrvAddr(namesrvAddr);  //同样也要设置NameServer地址
        producer.setInstanceName(instanceName);//初始化名字
        producer.setSendMsgTimeout(time);//sendMsgTimeout = 3000; producer.setSendMsgTimeout(time)
        producer.setMaxMessageSize(size);//maxMessageSize = 1024 * 128; producer.setMaxMessageSize(size)
        producer.start();
        log.info("DefaultMQProducer init success.");
    }

    public void destroy(){
        log.info("start destroy DefaultMQProducer...");
        producer.shutdown();
        log.info("DefaultMQProducer destroy success.");
    }

}
